package com.gzedu.module.controller.role;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gzedu.module.faced.role.RoleFacade;
import com.gzedu.module.modle.role.Role;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Resource
	private RoleFacade roleFacade;
	
	/**
	 * 新增角色信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response){
		return "/views/role/roleForm";
	}
	
	/**
	 * 发送需要修改的信息到修改页面
	 * @param request
	 * @param response
	 * @param model
	 * @param role
	 * @return
	 */
	@RequestMapping("/update")
	public String update(HttpServletRequest request, HttpServletResponse response,Model model,Role role){
		Role roles = roleFacade.getRoleById(role.getId());
		if(roles != null){
			model.addAttribute("id", roles.getId());
			model.addAttribute("roleName", roles.getRoleName());
			model.addAttribute("roleCode", roles.getRoleCode());
		}
		return "/views/role/roleForm";
	}
	
	/**
	 * 保存/修改信息
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(HttpServletRequest request, HttpServletResponse response,Role role){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("flag", roleFacade.save(role));
		return map;
	}
}
